package com.ihr360.cnb.module.insurance.controller;

import com.google.common.collect.Lists;
import com.ihr360.cnb.module.insurance.service.InsuranceAggregateService;
import com.ihr360.cnb.module.insurance.service.StaffIdCheckService;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.insurance.entity.vo.BenefitListEntity;
import com.ihr360.insurance.entity.vo.StaffBenefitEntity;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-01-25 15:32:00.
 */
@RestController
@RequestMapping("/benefit")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.MANDATORY_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.MANDATORY_NAME)
public class InsuranceAggregateController {

    @Autowired
    private StaffIdCheckService staffIdCheckService;

    @Autowired
    private InsuranceAggregateService insuranceAggregateService;

    @FunctionDefinition(functionName = "社保公积金信息保存", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/insurance/save", httpMethod = "POST", notes = "社保公积金信息保存")
    @RequestMapping(value = "/insurance/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveStaffInsurance(AuthUserInfo userInfo, @RequestBody StaffBenefitEntity staffBenefitEntity) {
        if (staffBenefitEntity == null || staffBenefitEntity.getStaffId() == null) {
            throw new Ihr360Exception(null, "员工社保信息不可为空");
        }
        staffIdCheckService.check(Lists.newArrayList(staffBenefitEntity.getStaffId()));
        insuranceAggregateService.saveStaffInsurance(userInfo.getCompanyId(), staffBenefitEntity);
    }

    @FunctionDefinition(functionName = "公积金社保方案查询", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "/insurance/query", httpMethod = "GET", notes = "公积金社保方案查询")
    @RequestMapping(value = "/insurance/query", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public BenefitListEntity queryInsurance(AuthUserInfo userInfo) {
        return insuranceAggregateService.queryInsurance(userInfo.getCompanyId());
    }

}
